<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\common\model;
use think\facade\Log;
use think\Db;

class TeacherInfo extends Common
{
    protected $pk = '';
    
    /**
     * 设置csv header
     * @return array
     */
    public function csvHeader()
    {
        return [
            [
                'id'   => 'bm',
                'desc' => '部/院',
            ],
            [
                'id'   => 'zy',
                'desc' => '专业',
            ],
            [
                'id'   => 'bj',
                'desc' => '班级',
            ],
            [
                'id'   => 'truename',
                'desc' => '辅导员姓名',
            ],
            [
                'id'   => 'mobile',
                'desc' => '辅导员手机',
            ]
        ];
    }

    /**
     * @param $post
     *
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function tableData($post)
    {
        if(isset($post['limit'])){
            $limit = $post['limit'];
        }else{
            $limit = config('paginate.list_rows');
        }

        $tableWhere = $this->tableWhere($post);
        $list = $this->field($tableWhere['field'])->where($tableWhere['where'])->order($tableWhere['order'])->paginate($limit);
        $data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型

        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = $list->total();
        $re['data'] = $data;

        return $re;
    }

    /**
     *  修改学生信息
     * User:tony
     * @param $data
     * @return array
     */
    public function saveData($data)
    {
        $result = [
            'status' => true,
            'msg' => '保存成功',
            'data' => []
        ];

        \think\facade\Request::token('__ZlServer_Token__', 'sha1');
        return $result;
    }

    //where搜索条件
    protected function tableWhere($post)
    {
        $where = [];
        if(isset($post['xh']) && $post['xh'] != ""){
            $where[] = ['xh', 'like', '%'.$post['xh'].'%'];
        }
        
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = ['xh ASC'];
        return $result;
    }


    /**
     * @param $list
     *
     * @return mixed
     */
    protected function tableFormat($list)
    {
        foreach($list as &$val){
           // $val['logo'] = _sImage($val['logo']);
           // $val['utime'] = getTime($val['utime']);
        }
        return $list;
    } 
     
    // 批量替换辅导员
    public function batchReplaceTeacher($data)
    {
        $return = [
            'status' => false,
            'msg'    => '替换失败',
            'data'   => ''
        ];

        // 变量
        $dep_id = $data['dep_id'];
        $classes = $data['classes'];
        $new_teacher_id = $data['new_teacher_id'];
        $list       = explode(",", $classes);

        // 获取学生组织信息
        $bmmc = '';
        $zymc = '';
        $bjmc = '';
        $dir = '';
        $truename = '';
        $mobile = '';
        
        // 校验所选数据是否存在已签收的学生
        foreach($list as $val) {
            $valList  = explode(";", $val);

            $bmmc = $valList[0];
            $zymc = $valList[1];
            $bjmc = $valList[2];
            $dir = $valList[3];
            $truename = $valList[4];
            $mobile = $valList[5];
  

            // 已生成计划的需要先删除计划
            $sql = "SELECT count(a.xh) as count
            FROM zlcw.zl_student_info a
                JOIN zlcw.zl_user b
                JOIN zlzhsf.pxsdm c
                JOIN zlzhsf.pbjdm d
                JOIN zlzhsf.pzydm e
                JOIN zlzhsf.pbmdm f
                JOIN zlcw.zl_view_get_dirs v
                JOIN zlcw.zl_book_apply_detail w
                JOIN zlcw.zl_book_apply u
            WHERE b.id = a.teacherId AND a.xh = c.XH AND c.BJDM = d.BJDM AND c.ZYDM = e.ZYDM AND c.BMDM = f.BMDM
                AND a.dir = v.id AND a.xh = w.xh AND w.applyid = u.id
                AND b.truename = '".$truename."'
                AND b.mobile = '".$mobile."'
                AND v.NAME = '".$dir."'
                AND d.BJMC = '".$bjmc."'
                AND e.ZYMC = '".$zymc."'
                AND f.BMMC = '".$bmmc."'
                AND u.year = (select value from zlcw.zl_config where name = 'year')
                AND u.season = (select value from zlcw.zl_config where name = 'season')
                AND u.is_ff = 0";
            $res = Db::query($sql);
            if($res[0]["count"]){
                $return['msg'] = "请先删除【".$bmmc."_".$zymc."_".$bjmc."】的发放申请！";
                return $return;
            }

            // 已签收的不能替换
            $sql = "SELECT count(a.xh) as count
                    FROM zlcw.zl_student_info a
                        JOIN zlcw.zl_user b
                        JOIN zlzhsf.pxsdm c
                        JOIN zlzhsf.pbjdm d
                        JOIN zlzhsf.pzydm e
                        JOIN zlzhsf.pbmdm f
                        JOIN zlcw.zl_view_get_dirs v
                        JOIN zlcw.zl_book_apply_detail w
                        JOIN zlcw.zl_book_apply u
                    WHERE b.id = a.teacherId AND a.xh = c.XH AND c.BJDM = d.BJDM AND c.ZYDM = e.ZYDM AND c.BMDM = f.BMDM
                        AND a.dir = v.id AND a.xh = w.xh AND w.applyid = u.id
                        AND b.truename = '".$truename."'
                        AND b.mobile = '".$mobile."'
                        AND v.NAME = '".$dir."'
                        AND d.BJMC = '".$bjmc."'
                        AND e.ZYMC = '".$zymc."'
                        AND f.BMMC = '".$bmmc."'
                        AND u.year = (select value from zlcw.zl_config where name = 'year')
                        AND u.season = (select value from zlcw.zl_config where name = 'season')
                        AND w.is_qs = 1";
            $res = Db::query($sql);
            if($res[0]["count"]){
                $return['msg'] = "【".$bmmc."_".$zymc."_".$bjmc."】存在已签收的学生！";
                return $return;
            }
        }

        $whereSql = '';
        if($dep_id != ''){
            if($dep_id == '00203'){
                $whereSql = " AND c.BMDM in ('00203', '00403')";
            }elseif($dep_id == '00204'){
                $whereSql = " AND c.BMDM in ('00204', '00404')";
            }elseif($dep_id == '00205'){
                $whereSql = " AND c.BMDM in ('00205', '00405')";
            }elseif($dep_id == '00210'){
                $whereSql = " AND c.BMDM in ('00210', '00410')";
            }elseif($dep_id == '00214'){
                $whereSql = " AND c.BMDM in ('00214', '00413')";
            }elseif($dep_id == '00216'){
                $whereSql = " AND c.BMDM in ('00216', '00414')";
            }elseif($dep_id == '00217'){
                $whereSql = " AND c.BMDM in ('00217', '00415')";
            }else{
                $whereSql = " AND c.BMDM = '".$dep_id."'";
            }
        }

        // 更新辅导员
        foreach($list as $val) {
            $valList  = explode(";", $val);

            $bmmc = $valList[0];
            $zymc = $valList[1];
            $bjmc = $valList[2];
            $dir = $valList[3];
            $truename = $valList[4];
            $mobile = $valList[5];
  
            // 这里直接调用SQL
            $sql = "UPDATE zl_student_info z
                    INNER JOIN (
                        SELECT a.xh
                        FROM zlcw.zl_student_info a
                            JOIN zlcw.zl_user b
                            JOIN zlzhsf.pxsdm c
                            JOIN zlzhsf.pbjdm d
                            JOIN zlzhsf.pzydm e
                            JOIN zlcw.zl_view_get_dirs v
                        WHERE b.id = a.teacherId AND a.xh = c.XH AND c.BJDM = d.BJDM AND c.ZYDM = e.ZYDM AND a.dir = v.id
                            AND b.truename = '".$truename."'
                            AND b.mobile = '".$mobile."'
                            AND v.NAME = '".$dir."'".$whereSql."
                            AND d.BJMC = '".$bjmc."'
                            AND e.ZYMC = '".$zymc."'
                    ) t ON z.xh = t.xh
                    SET z.teacherId = '".$new_teacher_id."'";
            $res = Db::query($sql);
        }

        $return['data']   = [];
        $return['status'] = true;
        $return['msg']    = '替换成功'; 
        \think\facade\Request::token('__ZlServer_Token__', 'sha1');
        return $return;
    }
}